<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>shell 练习-shell的getops | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>shell 练习-shell的getops</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个getinterface.sh 脚本可以接受选项[i，I]，完成下面任务：</p>
<ol>
<li>使用一下形式：getinterface.sh [-i interface | -I ip]</li>
<li>当用户使用-i选项时，显示指定网卡的IP地址；当用户使用-I选项时，显示其指定ip所属的网卡。</li>
</ol>
<p>例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">sh getinterface.sh -i eth0</div><div class="line">       sh getinterface.sh -I 192.168.0.1</div></pre></td></tr></table></figure>
<ol>
<li>当用户使用除[-i | -I]选项时，显示[-i interface | -I ip]此信息。</li>
<li>当用户指定信息不符合时，显示错误。（比如指定的eth0没有，而是eth1时）</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">ip add |awk -F <span class="string">":"</span> <span class="string">'$1 ~ /^[1-9]/ &#123;print $2&#125;'</span>|sed <span class="string">'s/ //g'</span> &gt; /tmp/eths.txt</div><div class="line">[ -f /tmp/eth_ip.log ] &amp;&amp; rm -f /tmp/eth_ip.log</div><div class="line"><span class="keyword">for</span> eth <span class="keyword">in</span> `cat /tmp/eths.txt`</div><div class="line"><span class="keyword">do</span></div><div class="line">    ip=`ip add |grep -A2 <span class="string">": <span class="variable">$eth</span>"</span> |grep inet |awk <span class="string">'&#123;print $2&#125;'</span> |cut -d <span class="string">'/'</span> -f 1`</div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$eth</span>:<span class="variable">$ip</span>"</span> &gt;&gt; /tmp/eth_ip.log</div><div class="line"><span class="keyword">done</span></div><div class="line"></div><div class="line">useage()</div><div class="line">&#123;</div><div class="line">    <span class="built_in">echo</span> <span class="string">"Please useage: <span class="variable">$0</span> -i 网卡名字 or <span class="variable">$0</span> -I ip地址"</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">wrong_eth()</div><div class="line">&#123;</div><div class="line">    <span class="keyword">if</span> ! grep -q <span class="string">"<span class="variable">$1</span>"</span> /tmp/eth_ip.log</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"请指定正确的网卡名字"</span></div><div class="line">    <span class="built_in">exit</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">wrong_ip()</div><div class="line">&#123;</div><div class="line">    <span class="keyword">if</span> ! grep -qw <span class="string">"<span class="variable">$1</span>"</span> /tmp/eth_ip.log</div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"请指定正确的ip地址"</span></div><div class="line">    <span class="built_in">exit</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> -ne 2 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    useage</div><div class="line">    <span class="built_in">exit</span></div><div class="line"><span class="keyword">fi</span></div><div class="line"></div><div class="line"><span class="keyword">case</span> <span class="variable">$1</span> <span class="keyword">in</span></div><div class="line">    -i)</div><div class="line">    wrong_eth <span class="variable">$2</span> </div><div class="line">    grep <span class="variable">$2</span> /tmp/eth_ip.log |awk -F <span class="string">':'</span> <span class="string">'&#123;print $2&#125;'</span></div><div class="line">    ;;</div><div class="line"></div><div class="line">    -I)</div><div class="line">    wrong_ip <span class="variable">$2</span></div><div class="line">    grep <span class="variable">$2</span> /tmp/eth_ip.log |awk -F <span class="string">':'</span> <span class="string">'&#123;print $1&#125;'</span></div><div class="line">    ;;</div><div class="line"></div><div class="line">    *)</div><div class="line">    useage</div><div class="line">    <span class="built_in">exit</span></div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Shell 练习/52.shell 练习-shell的getops/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
